<?php

use Illuminate\Support\Facades\Redirect;
use ClassPreloader\Config;
/*
|--------------------------------------------------------------------------
| Application & Route Filters
|--------------------------------------------------------------------------
|
| Below you will find the "before" and "after" events for the application
| which may be used to do any work before or after a request into your
| application. Here you may also register your custom route filters.
|
*/

App::before(function($request)
{
	//
});


App::after(function($request, $response)
{
	//
});

/*
|--------------------------------------------------------------------------
| Authentication Filters
|--------------------------------------------------------------------------
|
| The following filters are used to verify that the user of the current
| session is logged into this application. The "basic" filter easily
| integrates HTTP Basic authentication for quick, simple checking.
|
*/

Route::filter('auth', function()
{
	if (Auth::guest())
	{
		if (Request::ajax())
		{
			return Response::make('Unauthorized', 401);
		}
// 		Redirect::guest() 在重定向时会将当前url保存到session中，这样可以在登陆以后，使用Redirect::intended()方法跳转到之前的页面继续业务。
		return Redirect::guest('/')->with("login",true);
		//return Redirect::route('profile', array('user' => 1));
	}
});


Route::filter('auth.basic', function()
{
	return Auth::basic();
});

/*
|--------------------------------------------------------------------------
| Guest Filter
|--------------------------------------------------------------------------
|
| The "guest" filter is the counterpart of the authentication filters as
| it simply checks that the current user is not logged in. A redirect
| response will be issued if they are, which you may freely change.
|
*/

Route::filter('guest', function()
{
	if (Auth::check()) return Redirect::to('/');
});

/*
|--------------------------------------------------------------------------
| CSRF Protection Filter
|--------------------------------------------------------------------------
|
| The CSRF filter is responsible for protecting your application against
| cross-site request forgery attacks. If this special token in a user
| session does not match the one given in this request, we'll bail.
|
*/

Route::filter('csrf', function()
{
	if (Session::token() !== Input::get('_token'))
	{
		throw new Illuminate\Session\TokenMismatchException;
	}
});

Route::filter('admin', function($route, $request, $value = null)
{
	$routeName = $route->getName();
	//验证用户是否已经登录
	if (Auth::check()){
		$user = Auth::user();//获取当前登录的用户
		if (!$user->admin){//后台管理员
			//重定向到首页
			return Redirect::to("/");
		}
	} else {
		if ($routeName != 'showlogin' && $routeName != 'login'){
			return Redirect::route("showlogin");
		}
	}
	//获取当前的route名称
	//Log::info("cur-route:".Route::currentRouteName());
});

/**
 * Composer views
 */
View::composer('layouts.admin', function($view){
	$view->with('title',"蔓茂花廊后台管理");
});

View::composer(array('layouts.front','layouts.mobile',"layouts.ios"), function($view){
	$view->with('title',"MAGGIE MAO The Floral Art | 蔓茂花廊");
	//查询类别
	$tree = ProductCategorys::tree();
	$view->with("tree",$tree);
	
	$projects = Project::tree();
	$view->with("projects", $projects);
	//查询通知
	$time = time();
	$notice = Notice::orderBy("id","desc")->where("end",">=",date("y-m-d",$time))->first();
	$view->with("notice",$notice);
	//绑定用户
});

/**extend blade**/
Blade::extend(function($view, $compiler){
	$pattern = $compiler->createMatcher('config');
	return preg_replace($pattern, '$1<?php echo Config::get($2); ?>', $view);
});